package by.grsu.ra.mls;
import by.grsu.ra.mls.model.RadioListItem;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.exception.EmptyFileSystemException;
import by.grsu.hdm.exception.NonExistentFileIdException;
import by.grsu.hdm.model.MediaPlayerStatus;
import by.grsu.hdm.software.MediaPlayer;

public class MediaLibraryServlet extends HttpServlet {
	private static final String ID = "id";
	private static final String USB_FLASH_DRIVE_NAME = "MUSIC-4GB";
	private static final String PID = "MP#001";
	private static final String CONTENT_TYPE = "text/html";
	
	private MediaPlayer mediaPlayer;
	private List<RadioListItem> items;
       
    public MediaLibraryServlet() {
        super();
        initMediaPlayer();
    }
    
    private void initMediaPlayer() {
    	mediaPlayer = new MediaPlayer(PID);
        try {
			mediaPlayer.setSource(new UsbFlashDrive(USB_FLASH_DRIVE_NAME));
			mediaPlayer.play();
			
			items = new ArrayList<RadioListItem>();
			for (String file : mediaPlayer.getFilesQueue()) {
				items.add(new RadioListItem(mediaPlayer.getFilesQueue().indexOf(file) + 1, file));
			}
		} catch (EmptyFileSystemException e) {
			e.printStackTrace();
		}
    }

	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
        
        PrintWriter out = response.getWriter();
        
        printHeader(out);
        
        String id = request.getParameter(ID);
    	if(id!=null) {
			try	{
	    		mediaPlayer.playById(Integer.parseInt(id));
			}
			catch(NumberFormatException e) {
		    	out.append("<p style=\"color:red\">You entered invalid file id!</p>");
		    }
		    catch(NonExistentFileIdException e) {
		    	out.append("<p style=\"color:red\">You entered non-existent file id!</p>");
		    }		
    	}
    	printNowPlaying(out, mediaPlayer.getMediaPlayerStatus());
        
        printForm(out, mediaPlayer.getFilesQueue());
        printFooter(out);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private void printNowPlaying(PrintWriter out, MediaPlayerStatus mediaPlayerStatus) {
		out.append("	<p>");
		out.append("		" + mediaPlayerStatus.getMessage());	
		out.append("	</p>");
	}
	
	private void printForm(PrintWriter out, List<String> filesQueue) {
		out.append("	<form action=\"MediaLibraryServlet\" method=\"POST\">");
		out.append(			"You can select a file to play from the list below:");
		out.append("		<p>");
		
		for (RadioListItem item : items) {
			out.append("			<input type=\"radio\" name=\"id\" value=\"" + item.getId() + "\" >" + item.getValue() + "<br />");
		}
		
		out.append("		</p>");
		out.append("		or");
		out.append("		<p>");
		out.append("			<label for=\"id\">Enter the ID of media file manually:</label>");
		out.append("			<input type=\"text\" size=\"10\" name=\"id\" id=\"id\" />");
		out.append("		</p>");
		out.append("		<input type=\"submit\" />");
		out.append("	</form>");
	}
	
	private void printHeader(PrintWriter out) {
    	out.append("<html>");
    	out.append("	<head>");
    	out.append("		<title>MediaLibraryServlet project</title>");
    	out.append("	</head>");
    	out.append("	<body>");
    	out.append("		<p>");
    	out.append("			<h2>MediaLibraryServlet</h2>");
    	out.append("		</p>");
    }
    
    private void printFooter(PrintWriter out) {
    	out.append("	</body>");
    	out.append("</html>");
    }
}
